﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AirManager.Entities;
using NHibernate.Criterion;

namespace AirManager.Repository
{
    public class FleetRepository : RepositoryBase <Fleet>
    {
        public Fleet BuscarNome(string xnome)
        {
            var sql = CurrentSession.CreateCriteria(typeof(Fleet)).List<Fleet>().Where(x => x.Nome == xnome);
            return sql.FirstOrDefault();
        }


        public void DeleteFleetNotPilot(Fleet f)
        {
            var repo = new FleetRepository();
            var repopilot = new PilotRepository();
            f.Pilots.Clear();
            var pilots = CurrentSession.CreateCriteria<Pilot>().CreateAlias("Fleets", "fp")
                .Add(Restrictions.Eq("fp.Id", f.Id)).List<Pilot>();

            foreach (var pilot in pilots)
            {
                pilot.Fleets.Remove(f);
                repopilot.Save(pilot);
            }
            repo.Delete(f);
         }
        
    }

    


}
